﻿<script type="text/javascript">


    function RevistAddMainGrid() {
        $('#txtFillPerson').textbox('readonly', true);
        $('#txtFillDate').datebox({
            readonly: false,
            required: true,
            onSelect: function (date) {


                var datez = date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate();
                var datenow = new Date();          //现在时刻
                var datenowz = datenow.getFullYear() + "-" + (datenow.getMonth() + 1) + "-" + datenow.getDate();

                var dateTime = datez.replace(/-/g, "/");
                var nowTime = datenowz.replace(/-/g, "/");

                var date1 = Date.parse(dateTime);
                var date2 = Date.parse(nowTime);

                //当前日期减去勾选的日期
                var dayas = Math.ceil((date2 - date1) / (24 * 60 * 60 * 1000));
                if (dayas > 45 || dayas <= -1) { //2019-07-29 plq 报表日期，现在只能选15天内，放宽到45日
                    $.messager.alert("系统提示", "日报日期请确定为45天内!", "warning");
                    $('#txtFillDate').datebox('clear');
                }
            }
        });

        $('#txtFillPerson').textbox('setValue', DworkID);

        $('#txtBanCi1').combobox({
            valueField: 'ID',
            textField: 'Name',
            method: 'get',
            editable: false,
            panelHeight: 'auto',
            data: [

            {
                ID: '8小时',
                Name: '8小时'
            },
            {
                ID: '12小时日班',
                Name: '12小时日班'
            },
            {
                ID: '12小时夜班',
                Name: '12小时夜班'
            },
            //],
        {
            ID: '24小时班',
            Name: '24小时班'
        }
        ],//新增‘24小时班’ 2017-03-20 周志猛
            //当选中的时候去数据库中查询，当前日期的当前班次是否已经存在。如果有，提示  !!!!!!!!!!!!!!!!!!!!!!!!!!!
            onSelect: function (rec) {

            }
        });
        $('#txtBanCi1').combobox('readonly', false);
        if (EditRowID.length > 1) {
            $('#txtBanCi1').combobox('setValue', EditBanci)
            $('#txtBanCi1').combobox('readonly', true);

        }



        $('#RevisitTaskList').datagrid({
            //width: 'auto',
            //height: 'auto',
            height: 440,
            fitColumns: false, //真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
            loadMsg: '数据加载中,请稍后……',
            singleSelect: true,
            striped: true,
            pagination: false,//启用分页，默认每页10行
            rownumbers: false,//显示页码，默认 提供 10 - 50 的页容量选择下拉框
            pageSize: 15,//设置 页容量5
            pageList: [10, 15, 20],//设置 页容量下拉框

            columns: [[
           // { field: "taskCode", hidden: true },
            { field: "ReportID", hidden: true },
             { field: "TaskCode", hidden: true },
            //{ field: "RevisitState", title: "回访状态", width: '3%', align: "center" },
                { field: "StationName", title: "分站", width: '5%', align: "center" },
                { field: "RealAmbulanceSign", title: "车辆", width: '6%', align: "center" },
                { field: "Driver", title: "司机", width: '5%', align: "center" },
                { field: "Doctor", title: "医生", width: '5%', align: "center" },
                { field: "Nurse", title: "护士", width: '5%', align: "center" },
                { field: "Stretcher", title: "担架员", width: '5%', align: "center" },
                { field: "AlarmTel", title: "呼救电话", width: '8%', align: "center" },
                { field: "LocalAddr", title: "现场地址", width: '19%', align: "center" },
                { field: "SendAddr", title: "送往地址", width: '10%', align: "center" },
                { field: "PatientName", title: "患者姓名", width: '6%', align: "center" },
                { field: "AlarmReason", title: "主诉", width: '8%', align: "center" },
                { field: "StartTaskTime", title: "生成任务时刻", width: '11%', align: "center", formatter: $.formatTime },
                { field: "DispatcherName", title: "责任调度员", width: '5%', align: "center" }

            ]],
            toolbar: [
              {

                  id: 'btnDetailDelete',
                  text: '删除已选',
                  iconCls: 'icon-custom-Delete',
                  handler: function () {
                      var selected = $('#RevisitTaskList').datagrid('getSelected');
                      if (selected) {

                          deleteDeliveryDetailJsondate(selected.TaskCode);
                      } else {
                          $.messager.alert('提示', '请选择数据行', 'info', function () {
                          });
                      }
                  }

              }, '-',
               {
                   id: 'btnAdd',
                   text: '添加任务',
                   iconCls: 'icon-add',
                   handler: function () {
                       $('#RevisitAddTask').dialog('open');    //打开选择任务弹层页
                       PickTaskGrid();
                   }
               }
            ]
        });


        $("#RevisitTaskList").datagrid("loadData", Materials); //将所有勾选数据加载进来

        var rows = $('#RevisitTaskList').datagrid('getRows'); //获取选中的行
        $("#total").html(rows.length);//选中的行数赋给ID 为total的span标签
       // $("#txtFillDate").datebox('setValue', $.GetToday());
    }

    //删除已选
    function deleteDeliveryDetailJsondate(ecode) {
        var obj;

        $.each(Materials, function (i, info) {
            if (ecode == info.TaskCode) { obj = info; }

        });
        Materials.remove(obj); //从勾选行数据数组移除当前行数据
        $("#RevisitTaskList").datagrid("loadData", Materials); //重新将所有勾选数据加载进来

        changeRevisitViewNum();
        //  setDeliveryViewNumCheck();
        var rows = $('#RevisitTaskList').datagrid('getRows'); //获取选中的行
        $("#total").html(rows.length);//选中的行数赋给ID 为total的span标签

    }

    //提交了
    function submitRevisitDetailForm() {
        //$('#btnDelivery').linkbutton('disable');
        var isValid = $("#RevisitAddMain").form("validate");
        if (isValid) {

            var jsData = new Array();
            var i = 0;
            var rows = $('#RevisitTaskList').datagrid('getRows'); //获取所有行的数据
            

            //add 2019-06-17 plq 增加工作日期字段（取字段 生成任务时刻 中的最小值）
            var workDate = $.formatTime(rows[0].StartTaskTime, 0); //初始设置第一行数据中的生成任务时刻为最小值

            $(rows).each(function () {
                var obj = new Object();
                //obj.ID = rows[i].ID;
                obj.taskCode = rows[i].TaskCode;
                obj.ReportState = "已勾选";
                obj.TaskDate = rows[i].TaskDate;
                obj.Station = rows[i].StationName;
                obj.AmbulanceNumber = rows[i].RealAmbulanceSign;
                obj.AmbulanceCode = rows[i].AmbulanceCode;
                obj.AlarmTel = rows[i].AlarmTel;
                obj.LinkCall = rows[i].LinkCall;
                obj.LinkMan = rows[i].LinkMan;
                obj.PatientName = rows[i].PatientName;
                obj.Sex = rows[i].Sex;
                obj.Age = rows[i].Age;
                obj.AlarmReason = rows[i].AlarmReason;
                obj.LocalAddr = rows[i].LocalAddr;
                obj.SendAddr = rows[i].SendAddr;
                obj.StartTaskTime = $.formatTime(rows[i].StartTaskTime, i);

                //add 2019-06-17 plq遍历比较大小，若当前值比初始最小值小，则赋当前值为最小值
                if (getMinDate(workDate, obj.StartTaskTime)) {  
                    workDate = obj.StartTaskTime
                } 

                obj.DispatcherWorkID = rows[i].DispatcherWorkID;
                obj.DispatcherName = rows[i].DispatcherName;


                obj.Driver = rows[i].Driver;
                obj.DriverWorkID = rows[i].DriverWorkID;
                obj.Doctor = rows[i].Doctor;
                obj.DoctorWorkID = rows[i].DoctorWorkID;
                obj.Nurse = rows[i].Nurse;
                obj.NurseWorkID = rows[i].NurseWorkID;
                obj.Stretcher1Name = rows[i].Stretcher;
                obj.Stretcher1WorkID = rows[i].StretcherWorkID;
                obj.SendTime = rows[i].DrivingTime;
                obj.CompeletedTime = rows[i].CompletedTime;

                jsData.push(obj);
                i++;
            });

            console.log(workDate);
            workDate = workDate.split(" ")[0]; //只取日期，不要时刻
            console.log(workDate);


            $.ajax({
                type: 'POST',
                url: '@Url.Content("~/DD/DriverReport/SavaPickTask")',
                data:
                    {
                        ReportDate: $('#txtFillDate').datebox('getValue'),
                        EditRowID: EditRowID,
                        AgentWorkID: DworkID,
                        LastModifier: LastModifier,
                        BanCi: $('#txtBanCi1').combobox('getValue') ? $('#txtBanCi1').combobox('getValue') : "",
                        AmbulanceCode: $("#txtAmbulance").combobox('getValue') ? $("#txtAmbulance").combobox('getValue') : "",
                        AmbulanceNumber: $("#txtAmbulance").combobox('getText') ? $("#txtAmbulance").combobox('getText') : "",
                        sDeliveryDetailInfo: JSON.stringify(jsData),
                        WorkDate: workDate       //add 2019-06-17 plq 添加工作日期字段
                    },
                dataType: "json",
                success: function (jsData) {
                    $.handleJsonResult(jsData, function () {
                        Materials.length = 0;
                        $("#DriverReportMain").datagrid("reload"); //重新加载 司机日报表 页面主列表数据
                        $('#RevisitAddMain').dialog("close");
                        $('#RevisitAddTask').dialog("close");
                        EditRowID = "";
                        EditBanci = "";
                    }

                    );
                }
            });
        } else {
            $.showMsg("请填写必填项！", "提示");
        }
    }


    //add 2019-06-17 plq js判断两个日期的大小---形如："2009-10-20 14:38:40"
    function getMinDate( d1, d2)
    {
        //console.log(d1);
        //console.log(d2);
        var date1 = new Date(d1.replace(/-/g, "/")); //将2个日期字符串转为Date对象进行大小比较。
        var date2 = new Date(d2.replace(/-/g, "/"));
        //console.log(date1);
        //console.log(date2);
        return date1 > date2
    }
</script>

<div id="RevisitAddMain" class="easyui-dialog" title="提交任务" data-options="modal:true,collapsible:false,minimizable:false,maximizable:false,closed:true,resizable:true,iconCls:'icon-custom-cDelivery'" style="width: 98%; height: 520px; padding: 0px;">

    <div id="tbb" style="padding: 5px; height: auto">
        <table style="width:99%" cellpadding="0" cellspacing="0" border="0">
            <tr>
                <td valign="middle" width="8%">日报日期：</td>
                <td align="left" valign="middle" width="13%">
                    <input class="easyui-datebox" style="width: 110px;" id="txtFillDate">
                </td>
                <td valign="middle" width="8%">班次：</td>
                <td align="left" valign="middle" width="13%">
                    <input class="easyui-combobox" style="width: 110px;" id="txtBanCi1" data-options="required:true">
                </td>

                <td align="right" valign="middle" width="8%">填报人工号：</td>
                <td align="left" valign="middle" width="13%">
                    <input class="easyui-textbox" style="width: 110px" id="txtFillPerson">
                </td>
                <td valign="middle" width="20%">
                    已勾选【
                    <span id="total" style="width:150px; color:red"></span>
                    】任务。

                </td>
                <td width="12%">
                    &nbsp;&nbsp;
                    <a href="javascript:void(0)" id="btnSumbit" style="width:80px" class="easyui-linkbutton" data-options="iconCls:'icon-save'" onclick="submitRevisitDetailForm()">提交</a>
                </td>
                <td></td>
            </tr>
        </table>

    </div>
    <div data-options="region:'center'" style="background: #fafafa; overflow: hidden">
        <table id="RevisitTaskList" border="0"></table>
    </div>
</div>
